#include "WorkDokument.h"
#include "Logger/tri_logger.hpp"
#include "DocumentManager.h"
#include <QTextCursor>

WorkDokument::WorkDokument(DocumentManager* parent) : QTextDocument(parent){
	TRI_LOG_STR("WorkDokument ");
	connect(this, SIGNAL(contentsChange( int, int, int)), this, SLOT(contentsChange( int, int, int)));
	connect(this, SIGNAL(cursorPositionChanged ( const QTextCursor)),this,SLOT(cursorPositionChanged ( const QTextCursor)));
	document_manager = parent;
}

WorkDokument::~WorkDokument(){
	
	
}

void WorkDokument::contentsChange ( int position, int charsRemoved, int charsAdded ){
	
	TRI_LOG_STR("contentsChange");
	QTextDocumentFragment f;
	if(charsAdded >= 1){
		QTextCursor c = QTextCursor (this);
		c.beginEditBlock();
		c.setPosition (position, QTextCursor::MoveAnchor);
		c.setPosition (position+charsAdded, QTextCursor::KeepAnchor);
		TRI_LOG(c.position ());
		f =  c.selection ();
		c.clearSelection ();
		c.endEditBlock();
	}
	const QTextDocumentFragment f1 = QTextDocumentFragment(f);
	TRI_LOG(f1.toPlainText ().toAscii().constData ());	
	emit sendTextFragment(position, charsRemoved , f1);

}



void WorkDokument::cursorPositionChanged ( const QTextCursor & cursor ){
	TRI_LOG(cursor.position () );
}

